package com.jfeat.am.module.wegou.api.patch;

import com.baomidou.mybatisplus.plugins.Page;
import com.jfeat.am.common.annotation.Permission;
import com.jfeat.am.common.constant.tips.ErrorTip;
import com.jfeat.am.common.constant.tips.Ids;
import com.jfeat.am.common.constant.tips.SuccessTip;
import com.jfeat.am.common.constant.tips.Tip;
import com.jfeat.am.common.controller.BaseController;
import com.jfeat.am.core.jwt.JWTKit;
import com.jfeat.am.module.shopping.services.domain.model.ShoppingCartModel;
import com.jfeat.am.module.shopping.services.persistence.model.ShoppingCart;
import com.jfeat.am.module.wegou.api.permission.ProductStorePermission;
import com.jfeat.am.module.wegou.exception.WegouBizException;
import com.jfeat.am.module.wegou.services.crud.service.ProductStoreService;
import com.jfeat.am.module.wegou.services.domain.model.ViewHistoryModel;
import com.jfeat.am.module.wegou.services.domain.service.QueryProductService;
import com.jfeat.am.module.wegou.services.patch.WeGouPatchService;
import com.jfeat.am.module.wegou.services.persistence.model.Product;
import com.jfeat.am.module.wegou.services.persistence.model.ProductStore;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Code Generator on 2018-01-03
 */
@Api("料理店管理/足迹API")
@RestController
@RequestMapping("/api")
public class PatchEndpoint extends BaseController {

    @Resource
    WeGouPatchService weGouPatchService;
    @Resource
    ProductStoreService productStoreService;
    @Resource
    QueryProductService queryProductService;


    @ApiOperation(value = "批量删除料理店铺下的料理  店主" , response = Product.class)
    @PostMapping("/restaurant/{resId}/bulk/remove")
    public Tip batchDeleteProduct(@PathVariable long resId, @RequestBody Ids ids) {
        Long userId = JWTKit.getUserId(getHttpServletRequest());
        ProductStore restaurant = productStoreService.retrieveMaster(resId);
        if (!restaurant.getUserId().equals(userId)){
            return ErrorTip.create(WegouBizException.NO_PERMISSION.getCode(),
                    WegouBizException.NO_PERMISSION.getMessage());
        }
        return SuccessTip.create(weGouPatchService.batchDeleteProduct(userId, ids.getIds()));
    }

    @ApiOperation(value = "批量为料理店添加标签  店主", response = ProductStore.class)
    @PostMapping("/restaurant/{resId}/bulk/add")
    public Tip batchAddTag(@PathVariable long resId, @RequestBody Ids ids) {
        Long userId = JWTKit.getUserId(getHttpServletRequest());
        ProductStore restaurant = productStoreService.retrieveMaster(resId);
        if (!restaurant.getUserId().equals(userId)){
            return ErrorTip.create(WegouBizException.NO_PERMISSION.getCode(),
                    WegouBizException.NO_PERMISSION.getMessage());
        }
        return SuccessTip.create(weGouPatchService.batchAddTag(resId, ids.getIds()));
    }

    @ApiOperation(value = "管理员权限 设置店铺上下架", response = Product.class)
    @Permission(ProductStorePermission.ProductRestaurant_UPDATE)
    @PutMapping("/restaurant/{stireId}/upordown")
    public Tip upOrDownRestaurant(@PathVariable long stireId) {
        return SuccessTip.create(weGouPatchService.riseOrDownRestaurant(stireId));
    }

    @ApiOperation(value = "设置料理上下架", response = Product.class)
    @PutMapping("/restaurant/{stireId}/product/{productId}/upordown")
    public Tip upOrDownProduct(@PathVariable long stireId, @PathVariable long productId) {
        Long userId = JWTKit.getUserId(getHttpServletRequest());
        ProductStore restaurant = productStoreService.retrieveMaster(stireId);
        if (!restaurant.getUserId().equals(userId)){
            return ErrorTip.create(WegouBizException.NO_PERMISSION.getCode(),
                    WegouBizException.NO_PERMISSION.getMessage());
        }
        return SuccessTip.create(weGouPatchService.riseOrDownProduct(productId));
    }


    @ApiOperation(value = "个人足迹 时间 倒叙", response = ViewHistoryModel.class)
    @PostMapping("/user/view/history")
    public Tip viewHistory(Page page,
                           @RequestParam(name = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                           @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        long userId = JWTKit.getUserId(getHttpServletRequest());
        return SuccessTip.create(weGouPatchService.viewHistories(page,userId));
    }


    @ApiOperation(value = "购物车", response = ShoppingCart.class)
    @PostMapping("/restaurant/{resId}/cart")
    public Tip addProductToCart(@PathVariable long resId, @RequestBody List<ShoppingCartModel> models) {
        long userId = JWTKit.getUserId(getHttpServletRequest());
        return SuccessTip.create(weGouPatchService.addProductToCart(resId,userId,models));
    }

}